const jwt = require('jsonwebtoken')

const whiteList = [
  '/',
  '/api/users/login',
  '/api/users/register',
  '/api/users/loginCode',
  '/api/oauth/redirect',
  '/echart',
  '/api/code',
  '/api/loginCode',
  '/api/test-login',
  '/api/cache',
]
module.exports = () => {
  return async (ctx, next) => {
    if (whiteList.includes(ctx.path)) {
      await next()
    } else {
      const { token } = ctx.request.headers
      try {
        const userInfo = jwt.verify(token, ctx.app.config.privateKey)
        console.log('userInfo', userInfo)
        // 具体数据权限数据
        ctx.state.userInfo = userInfo
        await next()
      } catch (err) {
        console.log(err, 'err')
        ctx.body = { code: 409, data: err }
      }
    }
  }
}
